package com.yanqu.road.server.manger.union.listener;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.manger.ICrossUnionSyncListener;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.beautyescort.BeautyEscortMgr;
import com.yanqu.road.server.manger.rank.RankMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;

import java.util.ArrayList;
import java.util.List;

public class UnionUpGradeListener implements ICrossUnionSyncListener {

    @Override
    public void notifySync(int syncType, long playerId, CrossUnionInfo unionInfo, List<CrossUnionMember> memberList) {


        if(syncType == eUnionSyncType.UpGrade.getValue()){

            long serverId = GameServer.getInstance().getServerId();

            RankMgr.changeUnionRank(unionInfo);

            //商会升级
            BeautyEscortMgr.upgradeUnion(unionInfo);

         /*   for (CrossUnionMember member : memberList) {
                if(member.getServerId() == serverId){
                    GamePlayer player = GamePlayerMgr.getOnlinePlayer(member.getUserId());
                    if(player != null) {
                        player.notifyListener(eGamePlayerEventType.UnionExpRiseRank.getValue(), new UnionRiseRankArgs(UnionRiseRankArgs.NOTIFY_TYPE_NORMAL, BigInteger.valueOf(unionInfo.getExperience()), unionInfo.getUnionUid()));
                    }
                }
            }*/

            try {
                List<ActivityInfo> tempList = NormalActivityMgr.getUnionRiseRankActivityList();
                List<ActivityInfo> activityInfoList = new ArrayList<>();
                for (ActivityInfo activityInfo : tempList) {
                    if ((activityInfo.getType() == eActivityType.UnionRankActivity.getValue() && activityInfo.getChildType() == 2) ||
                            (activityInfo.getType() == eActivityType.CrossUnionActivity.getValue() && activityInfo.getChildType() == 3)) {
                        activityInfoList.add(activityInfo);
                    }
                }
                if (activityInfoList.size() == 0) {
                    return;
                }
                if (activityInfoList.size() > 1) {
                    //logger.error("one more union rank activity exist.");
                }
                ActivityInfo activityInfo = activityInfoList.get(0);
                int activityId = activityInfo.getActivityId();
                if (unionInfo.getRankActivityId() != activityId) {
                    unionInfo.setRankActivityId(activityId);
                    unionInfo.setRankAddExp(0L);
                }
                //unionInfo.setRankAddExp(unionInfo.getRankAddExp() + exp);
                UnionActivityMgr.addUnionActivityValue(activityId, unionInfo.getUnionUid(), unionInfo.getRankAddExp()); //排行
            } catch (Exception e) {
                //logger.error(e);
            }
        }

    }
}
